﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Infrastructure.Lib.Core;
using Infrastructure.Lib.Core.NPOIHelper;

namespace CrmSpider.Model
{
    public class CustomerInfoModel
    {
        private static Dictionary<string, Type> selectedDictionary = new Dictionary<string, Type>{
            {
                "x20_state",typeof(IndustryDictionary)
            },
            {
                "x21_state",typeof(BusinessDictionary)
            },
            {
                "x22_state",typeof(CustomerLevelDictionary)
            }
        };

        private List<string> selectStates = new List<string>
        {
            "x20_state",
            "x21_state",
            "x22_state"
        };

        [ColumnType(Name = "行政区划")]
        [Description("x15_state[\\s\\S]+?\"Text\": \"")]
        public string x15_state { get; set; }

        [ColumnType(Name = "上级客户")]
        [Description("x17_state[\\s\\S]+?\"Text\": \"")]
        public string x17_state { get; set; }

        [ColumnType(Name = "客户名称")]
        [Description("x18_state[\\s\\S]+?\"Text\": \"")]
        public string x18_state { get; set; }

        [ColumnType(Name = "客户邮编")]
        [Description("x19_state[\\s\\S]+?\"Text\": \"")]
        public string x19_state { get; set; }

        [ColumnType(Name = "行业")]
        [Description("x20_state[\\s\\S]+?\"SelectedValue\": \"")]
        public string x20_state { get; set; }

        [ColumnType(Name = "业务类别")]
        [Description("x21_state[\\s\\S]+?\"SelectedValue\": \"")]
        public string x21_state { get; set; }

        [ColumnType(Name = "客户级别")]
        [Description("x22_state[\\s\\S]+?\"SelectedValue\": \"")]
        public string x22_state { get; set; }

        [ColumnType(Name = "客户最终等级")]
        [Description("x23_state[\\s\\S]+?\"Text\": \"")]
        public string x23_state { get; set; }

        [ColumnType(Name = "客户网址")]
        [Description("x24_state[\\s\\S]+?\"Text\": \"")]
        public string x24_state { get; set; }

        [ColumnType(Name = "客户地址")]
        [Description("x25_state[\\s\\S]+?\"Text\": \"")]
        public string x25_state { get; set; }

        [ColumnType(Name = "客户简介")]
        [Description("x26_state[\\s\\S]+?\"Text\": \"")]
        public string x26_state { get; set; }

        public CustomerInfoModel(string oriStr)
        {
            var propertypes = this.GetType().GetProperties();
            foreach (var p in propertypes)
            {
                var value = oriStr.Substring2(p.ToDescription(), "\"", true);

                /*if (selectedDictionary.ContainsKey(p.Name))
                {
                    var selectedType = selectedDictionary[p.Name];
                    var instanceObj = selectedType.GetProperty("Instance").GetValue(null);
                    value = instanceObj.GetType().GetProperty("Item").GetValue(instanceObj, new object[] { value }) as string;
                }*/
                if (selectStates.Contains(p.Name))
                {
                    var selectStr = oriStr.Substring2($"{p.Name}[\\s\\S]+?\"X_Items\": ", ",\r", true);
                    var selects = selectStr.JsonToModels<List<List<string>>>();
                    var selectedValue = from s in selects where s[0] == value select s[1];
                    value = selectedValue.FirstOrDefault<string>();
                }
                p.SetValue(this, value);
            }
        }
    }
}
